package com.tz.crm.dao.impl;


import com.tz.crm.dao.DepartmentDao;
import com.tz.crm.entity.Department;
import com.tz.crm.entity.User;
import com.tz.crm.utils.Pager;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * Department Dao Impl
 */
@Repository
@Transactional
@SuppressWarnings("unchecked")
public class DepartmentDaoImpl extends BaseDaoImpl<Department, String>
        implements DepartmentDao {

    @Override
    public Department get(String deptName) {
        String hql = "from Department d where d.deptName=:name";
        Department dept = (Department) getSession().createQuery(hql).
                setParameter("name", deptName).uniqueResult();
        return dept;
    }

    @Override
    public List<User> getByName(String deptName) {
        DetachedCriteria dc = DetachedCriteria.forClass(Department.class);
        dc.add(Restrictions.eq("deptName", deptName));
        return findByPager(null, dc).getList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getByDepartmentName(String name, Pager pager) {
        if (pager == null) {
            pager = new Pager();
        }
        String hql = "from User u where u.dept.deptName=:name";
        List<User> users;
        users = getSession().createQuery(hql)
                .setParameter("name", name)
                .setFirstResult((pager.getPageNumber() - 1) * pager.getPageSize())
                .setMaxResults(pager.getPageSize())
                .list();
        return users;
    }

    @Override
    public Department getById(String id) {
        //return (Department) getSession().get(Department.class, id);

        String hql = "from Department d where d.id=:id";
        Department dept = (Department) getSession().createQuery(hql)
                .setParameter("id", id)
                .uniqueResult();
        checkChildren(dept);
        return dept;
    }

    @Override
    public Department getDeptWithChildren(String name) {
        String hql = "from Department d where d.deptName=:name";
        Department dept = (Department) getSession().createQuery(hql)
                .setParameter("name", name)
                .uniqueResult();


        checkChildren(dept);
        return dept;
    }

    /**
     * 根据部门检查是否存在子部门
     *
     * @param dept Department
     */
    private void checkChildren(Department dept) {
        if (null!=dept&&dept.getChildren().size() > 0) {
            Set<Department> set = dept.getChildren();
            set.forEach(this::checkChildren);
        }
    }

    @Override
    public Department getRoot() {
        String hql = "from Department d where d.parent is null";
        return (Department) getSession().createQuery(hql).uniqueResult();
    }

    @Override
    public List<Department> getAll() {
        String hql = "from Department d";
        return getSession().createQuery(hql).list();
    }

}
